package grib2handler.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {

	/**
	 * 本地时间转世界时间
	 * 
	 * @param localTime
	 * @return
	 */
	public static Date getUtcTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(Calendar.DST_OFFSET);
		cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		return cal.getTime();
	}
	
	/**
	 * 获取utc时间串
	 * @param formate
	 * @return
	 */
	public static String getUtcTimeStr(String formate) {
		SimpleDateFormat formatGmt0 = new java.text.SimpleDateFormat(formate);
		formatGmt0.setTimeZone(TimeZone.getTimeZone("GMT-0"));
		return formatGmt0.format(new Date());
	}

	/**
	 * 获取启动时间
	 * 
	 * @return
	 */
	public static int getStartHour() {
		int hour = 0;
		try {
			SimpleDateFormat df = new SimpleDateFormat("HH");
			df.setTimeZone(TimeZone.getTimeZone("GMT-0"));
			Date d = df.parse("00");
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			hour = cal.get(Calendar.HOUR_OF_DAY);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return hour;
	}
	
	public static void main(String[] args) {
		System.out.println(getUtcTimeStr("yyyyMMddHH"));
		getStartHour();
		System.out.println(getUtcTime());
	}
}
